import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.Set;

public class Base {

    private Element g =new Element("G:\\eclipse-workspace\\chuodl\\src\\main\\java\\cdl\\chuodl\\eletment.properties");
    private WebDriver driver;

    public Base() {
        super();

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--disable-gpu");


        System.setProperty("webdriver.chrome.driver", "E:\\chromedriver\\chromedriver.exe");
//		System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\qqq\\src\\main\\resources\\chromedriver.exe");
//		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        driver =new ChromeDriver();
//        driver = new ChromeDriver(options);



    }

    public void openPage() {

        String s=g.getConfig("path");
        System.out.println(s);
        driver.get(s);
//		driver.get("http://192.168.1.231:8182/base/a/login");
    }


    public void login() {
        String s=g.getConfig("path");
        System.out.println(s);
        driver.get(s);
        maxBrowser();
        input("用户名", "bj_admin");
        input("密码", "123456");
        click("登录");

    }

    /*元素等待，输入等待的元素和时间*/
    public WebElement wait(String s, int t) {
        WebDriverWait wait=new WebDriverWait(driver, t);
        By by=g.parseProFile(s);
        WebElement webElement = wait.until(ExpectedConditions.presenceOfElementLocated(by));
        return webElement;
    }
    /*元素点击*/
    public void click(String s) {
        By by=g.parseProFile(s);
        WebElement w=driver.findElement(by);
        w.click();
    }
    /*输入内容*/
    public void input(String element,String input) {
        By by=g.parseProFile(element);
        WebElement w=driver.findElement(by);
        w.sendKeys(input);


    }
    /*跳转frame*/
    public void switchFrame(String s) {
        driver.switchTo().frame(s);
    }
    /*跳回到默认frame*/
    public void switchDefaultFrame() {
        driver.switchTo().defaultContent();
    }
    /*没有id和name的切换frame*/
    public void switchFrameByXpath(String s) {
        driver.switchTo().frame(driver.findElement(By.xpath(s)));
    }
    /*最大浏览器*/
    public void maxBrowser() {
        driver.manage().window().maximize();
    }
    /*关闭浏览器*/
    public void closeBrowser() {
        driver.close();
    }
    /*得到当前窗口句柄*/
    public String getHandle() {
        String handle =driver.getWindowHandle();
        return handle;
    }
    /*得到所有窗口句柄*/
    public Set<String> getAllHandles() {
        Set<String> handles = driver.getWindowHandles();
        return handles;
    }
    /*等待时间*/
    public void sleep(int s) {
        try {
            Thread.sleep(s);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /*判断元素是否存在*/
    public boolean elementExist(String element) {
        By by=g.parseProFile(element);
        Boolean b=driver.findElement(by).isDisplayed();
        return b;
    }
    /*按位置选择下拉列表*/
    public void selectByIndex(String element,int index) {
        By by=g.parseProFile(element);
        Select select = new Select(driver.findElement(by));
        select.selectByIndex(index);
    }

}
